package com.huc;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * @author JET
 * @date 2020/6/19 16:05
 * mybatis代码生成类
 */
public class MybatisPlusGenerator {
    public static void main(String[] args) {
        new MybatisPlusGenerator().generator();
    }

    public static final String database = "notification_platform";
    public static final String user = "root";
    public static final String passwd = "password";
    public void generator(){
        AutoGenerator autoGenerator = new AutoGenerator();
        /**
         * 全局配置
         */
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java");
        globalConfig.setAuthor("JET");
        globalConfig.setOpen(false);
        globalConfig.setActiveRecord(true);
        globalConfig.setFileOverride(true);
        globalConfig.setEntityName("%sDao");
        globalConfig.setMapperName("%sMapper");
        globalConfig.setServiceName("%sService");
        globalConfig.setServiceImplName("%sServiceImpl");
        globalConfig.setControllerName("%sController");
        autoGenerator.setGlobalConfig(globalConfig);
        /**
         * 数据源配置
         */
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://192.168.220.110:3306/"+database+"?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername(user);
        dataSourceConfig.setPassword(passwd);
        autoGenerator.setDataSource(dataSourceConfig);
        /**
         * 策略配置，生成的表在这里配置
         */
        StrategyConfig stConfig = new StrategyConfig();
        stConfig.setCapitalMode(true);
        stConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        stConfig.setNaming(NamingStrategy.underline_to_camel);
        stConfig.setInclude("users");
        autoGenerator.setStrategy(stConfig);
        /**
         * 生成文件路径配置
         */
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.unicom.notificationplatform");
        packageConfig.setController("controller");
        packageConfig.setMapper("mapper");
        packageConfig.setService("serviceTemp");
        packageConfig.setServiceImpl("serviceTemp.Impl");
        packageConfig.setEntity("model.dao");
        autoGenerator.setPackageInfo(packageConfig);

        autoGenerator.execute();
    }
}
